package cn.com.bluemoon.daps.model.controller;

import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.BmStatus;
import cn.com.bluemoon.daps.model.dto.DapDataModelTableDto;
import cn.com.bluemoon.daps.model.dto.DapModelHistoryDto;
import cn.com.bluemoon.daps.model.entity.*;
import cn.com.bluemoon.daps.model.service.DapDataModelOperateHistoryService;
import cn.com.bluemoon.daps.model.service.DapDataModelVersionService;
import cn.com.bluemoon.daps.model.vo.SearchModelHistoryVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author karry
 * @Data 2021/7/27
 * @Time:15:57
 */
@RestController
@Slf4j
@RequestMapping("/history")
@Api(tags = {"模型历史表更"})
public class DapDataModelHistoryController {

    @Resource
    DapDataModelOperateHistoryService dapDataModelOperateHistoryService;

    @Resource
    DapDataModelVersionService dapDataModelVersionService;

    @PostMapping("/version/table/list")
    @ApiOperation("历史变更")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = String.class),
            @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = DapModelHistoryDto.class)})
    @BmPermission(value = "model:publish:list", name = "不通过")
    public ResultBean<IPage<DapModelHistoryDto>> versionHistory(@RequestBody @Valid SearchModelHistoryVo vo){

        // 查询历史版本
        IPage<DapModelHistoryDto> modelHistory = dapDataModelOperateHistoryService.getModelHistory(vo);
        return ResultBean.ok(modelHistory);
    }

    @GetMapping("/version/table/fields")
    @ApiOperation("表历史版本字段")
    @ApiResponses(value = {@ApiResponse(code = 500, message = "错误原因信息", response = String.class),
            @ApiResponse(code = 200, message = "ok", responseContainer = "List", response = DapDataModelTableField.class)})
    @BmPermission(value = "model:publish:list", name = "不通过")
    public ResultBean<List<DapDataModelTableField>> getFieldsList(@RequestParam("tableId") String tableId,
                                                                  @RequestParam("recordId") String recordId){

        // 查询历史版本
        List<DapDataModelVersion> versions = dapDataModelVersionService.getBaseMapper().selectList(new LambdaQueryWrapper<DapDataModelVersion>()
                .eq(DapDataModelVersion::getModelTableId, tableId).eq(DapDataModelVersion::getRecordId, recordId)
                .eq(DapDataModelVersion::getBmStatus, BmStatus.ON.getCode()));
        if(versions.isEmpty()){
            return ResultBean.error("当前表已经删除,没有字段数据");
        }
        String first = versions.stream().map(DapDataModelVersion::getVersionContent).findFirst().get();
        // 转成对象
        if(!StringUtils.isEmpty(first)){
            JSONObject newObject = JSON.parseObject(first);
            DapDataModelTableDto newTableDto = JSONObject.toJavaObject(newObject, DapDataModelTableDto.class);
            // 字段排序
            List<DapDataModelTableField> collect = newTableDto.getFieldList().stream().sorted(Comparator.comparing(DapDataModelTableField::getFieldRank)).collect(Collectors.toList());
            return ResultBean.ok(collect);
        }
        return ResultBean.error("当前表已经删除,没有字段数据");
    }
}
